/**
 * Created by HIAPAD on 2017/10/30.
 */
var Code=require("../utils/code");

//统计游戏下载次数
exports.downloadCount=function(req,res){
    var id=req.query.id;
    if(id===undefined){
        res.send({code:Code.paramsMissingCode,error:"参数错误"});
        return;
    }
    var sql="update gameInfo set downCount= downCount+1 where id=?";
    DB.query(sql,[id],function(err,result){
        if(err){
            res.send({code:Code.insideErrorCode,error:"服务器内部错误"});
            return;
        }
        res.send({code:Code.successCode,error:"",data:"操作成功"});
    })
}

//获取轮播图
exports.carousel=function(req,res){
    var sql="select * from carouselImage";
    DB.query(sql,[],function(err,result){
        if(err){
            res.send({code:Code.insideErrorCode,error:"服务器内部错误"});
            return;
        }
        var params=[];
        for(var i=0;i<result.length;i++){
            var item=result[i];
            params.push({imageurl:item.imageurl,hrefurl:item.hrefurl});
        }
        res.send({code:Code.successCode,error:"",data:params});
    })
}


//搜索
exports.searchGame=function(req,res){
    var gameName=req.query.gameName;
    var sql="select * from gameInfo where gameName like ?";
    if(gameName===undefined){
        //没有传参
        res.send({code:Code.paramsMissingCode,error:"参数错误"});
        return;
    }
    DB.query(sql,['%'+gameName+'%'],function(err,result){
        if(err){
            res.send({code:Code.insideErrorCode,error:"服务器内部错误"});
            return;
        }
        var params=[];
        for(var i=0;i<result.length;i++){
            var item=result[i];
            params.push({
                id:item.id,
                gameName:item.gameName,
                gameSize:item.gameSize,
                version:item.Version,
                updateTime:item.updateTime,
                iconUrl:item.iconUrl,
                downUrl:item.downUrl,
                downCount:item.downCount,
                gameType:item.gameType,
                desc:item.desc
            });
        }
        res.send({code:Code.successCode,error:"",data:params});
    })
}

//游戏列表
exports.gameList=function(req,res){
    var gameType=req.query.gameType;
    var callBack=req.query.callback;

    var sql;
    if(gameType===undefined){
        //没有参数
        sql="select * from gameInfo";
    }else{
        sql="select * from gameInfo where gameType=?";
    }

    DB.query(sql,gameType===undefined?[]:[gameType],function(err,result){
        if(err){
            if(callBack){
                // res.send(callBack({code:Code.insideErrorCode,error:"服务器内部错误"}));
                res.send(callBack+"("+JSON.stringify({code:Code.insideErrorCode,error:"服务器内部错误"})+")");
            }else{
                res.send({code:Code.insideErrorCode,error:"服务器内部错误"});
            }

            return;
        }
        var params=[];
        for(var i=0;i<result.length;i++){
            var item=result[i];
            params.push({
                id:item.id,
                gameName:item.gameName,
                gameSize:item.gameSize,
                version:item.Version,
                updateTime:item.updateTime,
                iconUrl:item.iconUrl,
                downUrl:item.downUrl,
                downCount:item.downCount,
                gameType:item.gameType,
                desc:item.desc
            });
        }
        if(callBack){
            res.send(callBack+"("+JSON.stringify({code:Code.successCode,error:"",data:params})+")");
            // res.send(callBack({code:Code.successCode,error:"",data:params}));
        }else{
            res.send({code:Code.successCode,error:"",data:params});
        }

    })
}